﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Linq.Expressions;
using WaterRevenueMS.WebUI.Common;
using System.ComponentModel.Composition;
using WaterRevenueMS.Core.Service.BasicSet.Impl;
using WaterRevenueMS.WebUI.Extension.Filters;
using WaterRevenueMS.Domain.Common.Models;
using WaterRevenueMS.Domain.ViewModels.BasicSet.ConsumptionAbnormal;
using WaterRevenueMS.Core.Service.BasicSet;
using WaterRevenueMS.Componnent.Common;
using WaterRevenueMS.Componnent.Entities.BasicSet;
using WaterRevenueMS.WebUI.Extension.MvcHelper;

namespace WaterRevenueMS.WebUI.Controllers
{
    [Export]
    [PartCreationPolicy(CreationPolicy.NonShared)]
    public class ConsumptionAbnormalController : AdminController
    {

        [Import]
        public IConsumptionAbnormalService ConsumptionAbnormalService { get; set; }

        [AdminLayoutAttribute]
        public ActionResult Index()
        {
            var model = new ConsumptionAbnormalModel();
            #region 上限
            
                model.Search.Shang_XBJItems.Add( new SelectListItem { Text = "--- 上限警报 ---", Value = "-1"});
                model.Search.Shang_XBJItems.Add(new SelectListItem { Text = "不启用", Value = "0" });
                model.Search.Shang_XBJItems.Add( new SelectListItem { Text = "固定值比较", Value = "1" });
                model.Search.Shang_XBJItems.Add(new SelectListItem { Text = "与上月用量比较(百分比)", Value = "2" });
                model.Search.Shang_XBJItems.Add(new SelectListItem { Text = "与上N月平均用量比较(百分比)", Value = "3" });
            
        
                model.Search.Xia_XBJItems.Add(new SelectListItem { Text = "--- 下限警报 ---", Value = "-1" });
                model.Search.Xia_XBJItems.Add(new SelectListItem { Text = "不启用", Value = "0" });
                model.Search.Xia_XBJItems.Add(new SelectListItem { Text = "固定值比较", Value = "1" });
                model.Search.Xia_XBJItems.Add(new SelectListItem { Text = "与上月用量比较(百分比)", Value = "2" });
                model.Search.Xia_XBJItems.Add(new SelectListItem { Text = "与上N月平均用量比较(百分比)", Value = "3" });
            #endregion
                return View(model);
        }
        [AdminPermission(PermissionCustomMode.Ignore)]
        public ActionResult List(DataTableParameter param)
        {
            int total = ConsumptionAbnormalService.ConsumptionAbnormals.Count(t => t.IsDeleted == false);

            var expr = BuildSearchCriteria();

            var filterResult = ConsumptionAbnormalService.ConsumptionAbnormals.Where(expr).Select(t => new ConsumptionAbnormalModel
            {
                Id = t.Id,
                Yi_CGZ = t.Yi_CGZ,
                Xia_XBJValue = t.Xia_XBJ,
                Yong_LXX =t.Yong_LXX,
                Yong_LSX = t.Yong_LSX,
                Xia_XBFB = t.Xia_XBFB,
                Xia_XGZ = t.Xia_XGZ,
                Shang_XBFB = t.Shang_XBFB,
                Shang_XGZ = t.Shang_XGZ,
                OrderNo = t.OrderNo,
               

            }).OrderBy(t => t.Id).Skip(param.iDisplayStart).Take(param.iDisplayLength).ToList();

            int sortId = param.iDisplayStart + 1;

            var result = from c in filterResult
                         select new[]
                        {
                            sortId++.ToString(), 
                            c.Yi_CGZ ,
                            c.Xia_XBJName,
                           // c.Xia_XBJValue.ToString(),
                            c.Yong_LXX.ToString() ,
                            c.Yong_LSX.ToString(),
                            c.Shang_XBJName,
                            c.Xia_XBFB.ToString(),
                            c.Xia_XGZ.ToString() ,
                            c.Shang_XBFB.ToString(),
                            c.Shang_XGZ.ToString() ,  
                            c.OrderNo.ToString(),
                            c.Id.ToString()
                        };

            return Json(new
            {
                sEcho = param.sEcho,
                iDisplayStart = param.iDisplayStart,
                iTotalRecords = total,
                iTotalDisplayRecords = total,
                aaData = result
            }, JsonRequestBehavior.AllowGet);
        }

        public ActionResult Create()
        {
            var model = new ConsumptionAbnormalModel();
            InitcabModel(model);
            return PartialView(model);
        }

        [HttpPost]
        [AdminOperateLog]
        public ActionResult Create(ConsumptionAbnormalModel model)
        {
            if (ModelState.IsValid)
            {
                OperationResult result = ConsumptionAbnormalService.Insert(model);
                if (result.ResultType == OperationResultType.Success)
                {
                    return Json(result);
                }
                else
                {
                    return PartialView(model);
                }
            }
            else
            {
                return PartialView(model);
            }
        }

        public ActionResult Edit(int Id)
        {
            var model = new ConsumptionAbnormalModel();
            InitcabModel(model);
            var entity = ConsumptionAbnormalService.ConsumptionAbnormals.FirstOrDefault(t => t.Id == Id);
            if (null != entity)
            {
                model = new ConsumptionAbnormalModel
                {
                    Id = entity.Id,
                    Yi_CGZ = entity.Yi_CGZ,
                    Xia_XBJValue =entity.Xia_XBJ,
                    Yong_LXX= entity.Yong_LXX,
                    Xia_XBFB = entity.Xia_XBFB,
                    Xia_XGZ = entity.Xia_XGZ,
                    Shang_XBJValue = entity.Shang_XBJ,
                    Yong_LSX = entity.Yong_LSX,
                    Shang_XBFB = entity.Shang_XBFB,
                    Shang_XGZ = entity.Shang_XGZ,
                    OrderNo = entity.OrderNo


                };
            }
            return PartialView(model);
        }

        [HttpPost]
        public ActionResult Edit(ConsumptionAbnormalModel model)
        {
            if (ModelState.IsValid)
            {
                OperationResult result = ConsumptionAbnormalService.Update(model);
                if (result.ResultType == OperationResultType.Success)
                {
                    return Json(result);
                }
                else
                {
                    return PartialView(model);
                }

            }
            else
            {
                return PartialView(model);
            }
        }


        public ActionResult Delete(int Id)
        {
            OperationResult result = ConsumptionAbnormalService.Delete(Id);
            return Json(result);
        }


        private Expression<Func<ConsumptionAbnormal, Boolean>> BuildSearchCriteria()
        {
            DynamicLambda<ConsumptionAbnormal> bulider = new DynamicLambda<ConsumptionAbnormal>();
            Expression<Func<ConsumptionAbnormal, Boolean>> expr = null;
            if (!string.IsNullOrEmpty(Request["Yi_CGZ"]))
            {
                var data = Request["Yi_CGZ"].Trim();
                Expression<Func<ConsumptionAbnormal, Boolean>> tmp = t => t.Yi_CGZ.Contains(data);
                expr = bulider.BuildQueryAnd(expr, tmp);
            }
            if (!string.IsNullOrEmpty(Request["Xia_XBJ"]) && Request["Xia_XBJ"] != "0")
            {
                var data = Request["Xia_XBJ"].Trim();
                Expression<Func<ConsumptionAbnormal, Boolean>> tmp = t => t.Xia_XBJ.ToString().Contains(data);
                expr = bulider.BuildQueryAnd(expr, tmp);
            }
            if (!string.IsNullOrEmpty(Request["Yong_LXX"]))
            {
                var data = Request["Yong_LXX"].Trim();
                Expression<Func<ConsumptionAbnormal, Boolean>> tmp = t => t.Yong_LXX.ToString().Contains(data);
                expr = bulider.BuildQueryAnd(expr, tmp);
            }
            if (!string.IsNullOrEmpty(Request["Xia_XBFB"]))
            {
                var data = Request["Xia_XBFB"].Trim();
                Expression<Func<ConsumptionAbnormal, Boolean>> tmp = t => t.Xia_XBFB.ToString().Contains(data);
                expr = bulider.BuildQueryAnd(expr, tmp);
            }
            if (!string.IsNullOrEmpty(Request["Xia_XGZ"]))
            {
                var data = Request["Xia_XGZ"].Trim();
                Expression<Func<ConsumptionAbnormal, Boolean>> tmp = t => t.Xia_XGZ.ToString().Contains(data);
                expr = bulider.BuildQueryAnd(expr, tmp);
            }
            if (!string.IsNullOrEmpty(Request["Shang_XBJ"]))
            {
                var data = Request["Shang_XBJ"].Trim();
                Expression<Func<ConsumptionAbnormal, Boolean>> tmp = t => t.Shang_XBJ.ToString().Contains(data);
                expr = bulider.BuildQueryAnd(expr, tmp);
            }
            if (!string.IsNullOrEmpty(Request["Yong_LSX"]))
            {
                var data = Request["Yong_LSX"].Trim();
                Expression<Func<ConsumptionAbnormal, Boolean>> tmp = t => t.Yong_LSX.ToString().Contains(data);
                expr = bulider.BuildQueryAnd(expr, tmp);
            }
            if (!string.IsNullOrEmpty(Request["Shang_XBFB"]))
            {
                var data = Request["Shang_XBFB"].Trim();
                Expression<Func<ConsumptionAbnormal, Boolean>> tmp = t => t.Shang_XBFB.ToString().Contains(data);
                expr = bulider.BuildQueryAnd(expr, tmp);
            }
            if (!string.IsNullOrEmpty(Request["Shang_XGZ"]))
            {
                var data = Request["Shang_XGZ"].Trim();
                Expression<Func<ConsumptionAbnormal, Boolean>> tmp = t => t.Shang_XGZ.ToString().Contains(data);
                expr = bulider.BuildQueryAnd(expr, tmp);
            }

            Expression<Func<ConsumptionAbnormal, Boolean>> tmpSolid = t => t.IsDeleted == false;
            expr = bulider.BuildQueryAnd(expr, tmpSolid);

            return expr;
        }
        private void InitcabModel(ConsumptionAbnormalModel model)
        {
            #region 上限

            model.Shang_XBJItems.Clear();
            model.Shang_XBJItems.Add(new SelectListItem { Text = "--- 上限警报 ---", Value = "-1" });
            model.Shang_XBJItems.Add(new SelectListItem { Text = "不启用", Value = "0" });
            model.Shang_XBJItems.Add(new SelectListItem { Text = "固定值比较", Value = "1" });
            model.Shang_XBJItems.Add(new SelectListItem { Text = "与上月用量比较(百分比)", Value = "2" });
            model.Shang_XBJItems.Add(new SelectListItem { Text = "与上N月平均用量比较(百分比)", Value = "3" });

            model.Xia_XBJItems.Clear();
            model.Xia_XBJItems.Add(new SelectListItem { Text = "--- 下限警报 ---", Value = "-1" });
            model.Xia_XBJItems.Add(new SelectListItem { Text = "不启用", Value = "0" });
            model.Xia_XBJItems.Add(new SelectListItem { Text = "固定值比较", Value = "1" });
            model.Xia_XBJItems.Add(new SelectListItem { Text = "与上月用量比较(百分比)", Value = "2" });
            model.Xia_XBJItems.Add(new SelectListItem { Text = "与上N月平均用量比较(百分比)", Value = "3" });
            #endregion



        }

    }
}
